package cn.mesmile.auth.common;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author zb
 * @date 2021/8/19 11:11
 * @Description
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理未登录的异常
     */
    @ResponseBody
    @ExceptionHandler(value = NotLoginException.class)
    public R handleNotLoginException(NotLoginException e) {
        String message = e.getMessage();
        return R.fail(ResultCode.UN_AUTHORIZED,"未认证，请登录");
    }

    /**
     * 处理没有角色的异常
     */
    @ResponseBody
    @ExceptionHandler(value = NotRoleException.class)
    public R handleNotRoleException(NotRoleException e) {
        return R.fail(ResultCode.UN_AUTHORIZED, ResultCode.UN_AUTHORIZED.message);
    }

    /**
     * 处理没有权限的异常
     */
    @ResponseBody
    @ExceptionHandler(value = NotPermissionException.class)
    public R handleNotPermissionException(NotPermissionException e) {
        return R.fail(ResultCode.UN_AUTHORIZED, ResultCode.UN_AUTHORIZED.message);
    }



}
